#pragma once
#ifndef __SINGAL_MANAGER_H__
#define __SINGAL_MANAGER_H__
//#include"tool/PreProcess.h"
#include"Signal.h"
#include<mutex>

class SignalManager
{
public:
	static SignalManager* getInstance()
	{
		m_instance = m_load.load(std::memory_order_relaxed);
		std::atomic_thread_fence(std::memory_order_acquire);
		if (m_instance == nullptr)
		{
			std::lock_guard<std::mutex> lock(m_mutex);
			m_instance = m_load.load(std::memory_order_relaxed);
			if (m_instance == nullptr)
			{
				m_instance = new  SignalManager;
				std::atomic_thread_fence(std::memory_order_release);
				m_load.store(m_instance, std::memory_order_release);
			}
		}
		return m_instance;
	}
public:
	Slot* add(const int& messageId, EventHandler* evHandler, const bool& once = false);

	Slot* add(const int& messageId, std::function<void(Json::Value&)>&, const bool& once = false);

	void dispatch(const int& messageId, Json::Value& message);
private:
	SignalManager();
	~SignalManager();

	std::map<int, Signal*> m_signals;

	static SignalManager* m_instance;
	static std::atomic<SignalManager*> m_load;
	static std::mutex m_mutex;
};

#endif // !__SINGAL_MANAGER_H__

